local hx__xiangle = fk.CreateSkill {
  name = "hx__xiangle",
}


hx__xiangle:addEffect("active", {
  name = "hx__xiangle",
  anim_type = "support",
  prompt = "#hx__xiangle",
  card_num = 0,
  target_num = 1,
  interaction = function(self, player)
    local choiceList = {
      "hx__xiangle_basic",
      "hx__xiangle_trick",
      "hx__xiangle_equip",
    }
    local choices = {}
    local markValue = player:getMark("@hx__xiangle_yu")
    if markValue > 1 then
      table.insertTable(choices, {choiceList[1]})
    end
    if markValue > 2 then
      table.insert(choices, choiceList[2])
    end

    if markValue > 3 then
      table.insert(choices, choiceList[3])
    end
    return UI.ComboBox { choices = choices, all_choices = choiceList }
  end,
  can_use = function(self, player)
    return player:usedEffectTimes(self.name, Player.HistoryPhase) < 2 and player:getMark("@hx__xiangle_yu") > 1
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected, selected_cards)
    if #selected > 0 then
      return false
    end
    if self.interaction.data == "hx__xiangle_basic" then
      return to_select:getMark("@@hx__xiangle_basic-round") == 0 and to_select:getMark("hx__xiangle-round") == 0
    end
    if self.interaction.data == "hx__xiangle_trick" then
      return to_select:getMark("@@hx__xiangle_trick-round") == 0 and to_select:getMark("hx__xiangle-round") == 0
    end
    if self.interaction.data == "hx__xiangle_equip" then
      return to_select:getMark("@@hx__xiangle_equip-round") == 0 and to_select:getMark("hx__xiangle-round") == 0
    end
    return true
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    local choice = self.interaction.data
    if choice == "hx__xiangle_basic" then
      room:removePlayerMark(player, "@hx__xiangle_yu", 2)
      room:addPlayerMark(target, "@@hx__xiangle_basic-round")
      room:addPlayerMark(player, "hx__xiangle-round")
    elseif choice == "hx__xiangle_trick" then
      room:removePlayerMark(player, "@hx__xiangle_yu", 3)
      room:addPlayerMark(target, "@@hx__xiangle_trick-round")
      room:addPlayerMark(player, "hx__xiangle-round")
    elseif choice == "hx__xiangle_equip" then
      room:removePlayerMark(player, "@hx__xiangle_yu", 4)
      room:addPlayerMark(target, "@@hx__xiangle_equip-round")
      room:addPlayerMark(player, "hx__xiangle-round")
    end
  end,
})

hx__xiangle:addEffect(fk.TargetConfirmed, {
  anim_type = "defensive",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and data.card.trueName == "slash" and (player:getMark("@@hx__xiangle_basic-round") ~= 0 or 
    player:getMark("@@hx__xiangle_trick-round") ~= 0 or player:getMark("@@hx__xiangle_equip-round") ~= 0)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if player:getMark("@@hx__xiangle_basic-round") ~= 0 then
      if data.from.dead or #room:askToDiscard(data.from, {
        min_num = 1,
        max_num = 1,
        include_equip = false,
        skill_name = hx__xiangle.name,
        cancelable = true,
        pattern = ".|.|.|.|.|basic",
        prompt = "#hx__xiangle-discard_basic:"..player.id,
      }) == 0 then
        data.nullified = true
      end
    end
    if player:getMark("@@hx__xiangle_trick-round") ~= 0 then
      if data.from.dead or #room:askToDiscard(data.from, {
        min_num = 1,
        max_num = 1,
        include_equip = false,
        skill_name = hx__xiangle.name,
        cancelable = true,
        pattern = ".|.|.|.|.|trick",
        prompt = "#hx__xiangle-discard_trick:"..player.id,
      }) == 0 then
        data.nullified = true
      end
    end
    if player:getMark("@@hx__xiangle_equip-round") ~= 0 then
      if data.from.dead or #room:askToDiscard(data.from, {
        min_num = 1,
        max_num = 1,
        include_equip = true,
        skill_name = hx__xiangle.name,
        cancelable = true,
        pattern = ".|.|.|.|.|equip",
        prompt = "#hx__xiangle-discard_equip:"..player.id,
      }) == 0 then
        data.nullified = true
      end
    end
  end,
})


hx__xiangle:addEffect(fk.HpRecover, {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(hx__xiangle.name) and player:getMark("@hx__xiangle_yu") < 10 and
    target:getMark("hx__xiangle_recovers-turn") == 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    player.room:addPlayerMark(player, "@hx__xiangle_yu", math.min(4, 10 - player:getMark("@hx__xiangle_yu")))
    player.room:addPlayerMark(target, "hx__xiangle_recovers-turn")
  end,
})

hx__xiangle:addEffect(fk.AfterDying, {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(hx__xiangle.name) and player:getMark("@hx__xiangle_yu") < 10
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    player.room:addPlayerMark(player, "@hx__xiangle_yu", math.min(4, 10 - player:getMark("@hx__xiangle_yu")))
  end,
})

hx__xiangle:addLoseEffect(function (self, player, is_death)
  player.room:setPlayerMark(player, "@hx__xiangle_yu", 0)
end)

return hx__xiangle
